package cmdrunner

import (
	"github.com/project-flogo/core/data/coerce"
)

type Settings struct {
}

type Input struct {
	Cmd     string `md:"cmd" json:"cmd"`
	Timeout string `md:"timeout" json:"timeout"`
}

func (r *Input) FromMap(values map[string]interface{}) error {
	cmd, err := coerce.ToString(values["cmd"])
	if err != nil {
		return err
	}
	r.Cmd = cmd
	return nil
}

func (r *Input) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"cmd": r.Cmd,
	}
}

type Output struct {
	Err    string `md:"err" json:"err,omitempty"`
	Stdout []byte `md:"stdout" json:"stdout,omitempty"`
	Stderr []byte `md:"stderr" json:"stderr,omitempty"`
}

func (r *Output) FromMap(values map[string]interface{}) error {
	errStr, err := coerce.ToString(values["err"])
	if err != nil {
		return err
	}
	r.Err = errStr
	stdout, err := coerce.ToBytes(values["stdout"])
	if err != nil {
		return err
	}
	r.Stdout = stdout
	stderr, err := coerce.ToBytes(values["stderr"])
	if err != nil {
		return err
	}
	r.Stderr = stderr
	return nil
}

func (r *Output) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"err":    r.Err,
		"stdout": r.Stdout,
		"stderr": r.Stderr,
	}
}
